package com.sharemarking.application.wechat.controller;

import com.sharemarking.application.common.entity.File;
import com.sharemarking.application.common.repository.FileRepo;
import com.sharemarking.application.common.utils.StringUtils;
import com.sharemarking.application.wechat.entity.Client;
import com.sharemarking.application.wechat.entity.Material;
import com.sharemarking.application.wechat.repository.MaterialRepo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Created by cjw on 2017/11/25.
 */

@Controller
@RequestMapping("material")
public class MaterialController {
    @Autowired
    private MaterialRepo materialRepo;

    @Autowired
    private FileRepo fileRepo;

    /**
     * 素材列表页
     * @param m
     * @param page
     * @param size
     * @param model
     */
    @RequestMapping(value = "index", method = RequestMethod.GET)
    @PreAuthorize("hasPermission('w:material', 1)")
    public void index(Material m, Integer page, Integer size, Model model){
        if (page == null){
            page = 0;
        }else
            page -= 1;
        if (size == null){
            size = 10;
        }
        Pageable req = new PageRequest(page, size);
        Page p = null;
        p = materialRepo.findAll(new Specification<Material>() {
            @Override
            public Predicate toPredicate(Root<Material> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder cb) {
                List<Predicate> predicates = new ArrayList<>();
                if (m.getName() != null){
                    predicates.add(cb.equal(root.get("name"), m.getName()));
                }
                predicates.add(cb.equal(root.get("delFlag").as(Boolean.class), false));
                return cb.and(predicates.toArray(new Predicate[] {}));
            }
        }, req);

        model.addAttribute("page", p);
        model.addAttribute("search", m);
    }

    /**
     * 获取代理表单
     */
    @RequestMapping(value = "{id}", method = RequestMethod.GET)
    public String get(Material obj, Model model) {
        if (obj.getId() != 0)
            obj = materialRepo.findOne(obj.getId());
        model.addAttribute("obj", obj);
        return "material/form";
    }

    /**
     * id==0 => 新增实体 id!=0 => 更新实体
     */
    @RequestMapping(value = "{id}", method = RequestMethod.POST)
    @Transactional
    @PreAuthorize("hasPermission('w:material',#target.id==0?8:4)")
    public String post(Material target, RedirectAttributes redirectAttributes, @RequestParam(value = "ids")Integer[] ids) {
        if (target.getId() == 0) {
            target.setName(target.getName());
            File file = fileRepo.findOne(ids[0]);
            target.setImgUrl(file.getFileUrl());

            materialRepo.save(target);
            redirectAttributes.addFlashAttribute("msg", "新增成功");
            return "redirect:index";
        } else {

            return "redirect:index";
        }
    }
}
